from fastapi import FastAPI,Form,HTTPException
import pymysql
app=FastAPI()
@app.post("/select")
def login(name:str=Form(...),pwd:str=Form(...)):
    # 用户与密码不可为空
    if name is None or name == "":
        raise HTTPException(400, "用户名不可为空")
    if pwd is None or pwd == "":
        raise HTTPException(400, "密码不可为空")
    # 链接数据库
    conn = pymysql.Connection(user="root", password="Aa1234567.", database="p")
    # 创建游标对象
    cursor = conn.cursor()
    # 执行SQL语句
    cursor.execute("select * from t_user where name = %s and pwd = %s", (name, pwd))
    # 创建语句
    a = cursor.fetchone()
    # 执行对象语句
    if a is None:
        conn.close()
        cursor.close()
        raise HTTPException(400, "登录失败")
    else:
        conn.close()
        cursor.close()
        return {"msg": "登录成功"}


